package com.example.a2207_zh.detail

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.example.a2207_zh.R
import com.example.a2207_zh.adapter.GiftAdapter
import com.example.a2207_zh.databinding.ActivityVideoDetailBinding
import com.example.a2207_zh.entity.GiftEntity
import com.example.a2207_zh.entity.VideoEntity
import com.example.a2207_zh.mvvm.BaseActivity

@Route(path = "/app/VideoDetailActivity")
class VideoDetailActivity : BaseActivity<ActivityVideoDetailBinding, VideoDetailViewModel>() {

    val list = mutableListOf<GiftEntity>()
    lateinit var giftAdapter: GiftAdapter


    @Autowired
    lateinit var video: VideoEntity
    override fun getLayout(): Int = R.layout.activity_video_detail

    override fun getViewModel(): Class<VideoDetailViewModel> = VideoDetailViewModel::class.java

    override fun initView() {
        ARouter.getInstance().inject(this)

        giftAdapter = GiftAdapter(list)
        binding.dlRvGift.adapter = giftAdapter
        binding.dlRvGift.layoutManager = GridLayoutManager(this,4)
        viewModel.gift()


        /* val videoEntity = intent.extras?.get("video") as VideoEntity*/
        binding.dlAuthname.text = video.authname
        binding.gsy.setUp(video.videopath, true, "")
        binding.gsy.startPlayLogic()
        binding.gsy.backButton.visibility = View.GONE

        //实现点赞功能
        binding.dlLove.setOnClickListener {
            binding.dlKsg.addLikeImages(
                R.drawable.baseline_favorite_24,
                R.drawable.baseline_favorite_border_24,
                R.drawable.baseline_star_24,
                R.drawable.baseline_star_border_24
            )
            binding.dlKsg.addFavor()
        }
        binding.dlGift.setOnClickListener {
            binding.dlRvGift.isVisible = true
            //加载礼物数据
        }
        //点击礼物列表item,实现发送礼物
        giftAdapter.setOnItemClickListener { adapter, view, position ->
            //添加礼物动画
            binding.dlGiftIv.isVisible = true
            Glide.with(this).load(list[position].giftpath).into(binding.dlGiftIv)

            val width = windowManager.defaultDisplay.width.toFloat()//屏幕宽度

            val translationX = ObjectAnimator.ofFloat(binding.dlGiftIv,"translationX",0f,width,0f)
            val translationY = ObjectAnimator.ofFloat(binding.dlGiftIv,"translationY",0f,200f,0f,200f,0f)
            val set = AnimatorSet()
            set.duration = 3000
            set.play(translationX).with(translationY)
            set.addListener(object :Animator.AnimatorListener{
                override fun onAnimationStart(animation: Animator) {
                }

                override fun onAnimationEnd(animation: Animator) {
                    binding.dlGiftIv.isVisible = false
                }

                override fun onAnimationCancel(animation: Animator) {
                }

                override fun onAnimationRepeat(animation: Animator) {
                }
            })
            set.start()

        }
    }

    override fun initData() {

        viewModel.giftSuccess.observe(this){
            if(it.code==200){
                list.clear()
                list.addAll(it.data)
                giftAdapter.notifyDataSetChanged()
            }
        }

    }

}